package com.ww.httpclient;

import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.io.IOException;

/**
 * 响应处理
 */
public class HttpClientResponseHandler {

    public static void main(String[] args) throws IOException, ParseException {
        /**
         * 在HttpClient中把响应封装成了CloseableHttpResponse对象，在此对象中可以获取如下数据
         * 1、getCode()获取响应状态
         * 2、getEntity()获取响应数据
         */
        int code = 200;
        CloseableHttpResponse response = null; // TODO
        if (code == HttpStatus.SC_OK) {
            System.out.println("请求响应成功");

            // 响应结果
            HttpEntity responseEntity = response.getEntity();
        }

        // HttpClient提供了EntityUtils工具，方便把HttpEntity转为字节数组或字符串
        // 转换为字符串
        String string = EntityUtils.toString(response.getEntity());
        // 转换为字节数组
        byte[] bytes = EntityUtils.toByteArray(response.getEntity());
    }
}
